package com.qm.struts.myhome;



import java.util.Date;
import java.util.List;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.qm.hibernate.Tblog;
import com.qm.hibernate.TblogDAO;
import com.qm.hibernate.Tpost;
import com.qm.hibernate.TpostDAO;
import com.qm.hibernate.Tuser;
import com.qm.hibernate.TuserDAO;

public class MyInfoAction implements Action {

	private Integer id;
	private String name;
	private String gender;
	private Date birthday;
	private String signature;
	private String address;
	private String phoneNumber;
	private String email;
	private String permission;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public String preProcess()
	{
		ActionContext ctx = ActionContext.getContext();
		Tuser oldUser  = (Tuser)ctx.getSession().get("user");
		TpostDAO pd = new TpostDAO();
		TblogDAO bd = new TblogDAO();
		List l = pd.findByProperty("tuser.id", oldUser.getId());
		List l2 = bd.findByProperty("tuser.id", oldUser.getId());
		ctx.getSession().put("myPost", l);
		ctx.getSession().put("myBlog", l2);
		List<Tblog> l3 = (List<Tblog>) ctx.getSession().get("myBlog");
		return SUCCESS;
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		ActionContext ctx = ActionContext.getContext();
		Tuser oldUser  = (Tuser)ctx.getSession().get("user");
		
		Tuser user = new Tuser();
		String   a=   new   String(getAddress().getBytes( "iso-8859-1"), "utf-8"); 
		String   n=    new   String(getName().getBytes( "iso-8859-1"), "utf-8"); 
		String   s=   new   String(getSignature().getBytes( "iso-8859-1"), "utf-8"); 
		String 	g = new   String(getGender().getBytes( "iso-8859-1"), "utf-8"); 
		String 	p = new   String(getPhoneNumber().getBytes( "iso-8859-1"), "utf-8"); 
		
		
		System.out.println(user.getBirthday());
		user.setAddress(a);
		user.setGender(g);
		user.setName(n);
		user.setSignature(s);	
		user.setPhoneNumber(p);
		user.setId(oldUser.getId());
		user.setBirthday(getBirthday());
		user.setPassword(oldUser.getPassword());
		user.setPermission(getPermission());
	
		TuserDAO ud = new TuserDAO();

		ud.update(user,oldUser.getId());
		
		ctx.getSession().put("user", user);
		
		return SUCCESS;
	}

}
